package com.she.iiicoursecenter.activity.template;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

import ken.app.helper.Helper;

import com.she.iiicoursecenter.iiiApplication;
import com.she.iiicoursecenter.manager.ResourceManager;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class TemplateActivity extends Activity {
	//
	View mCurrentView;	// Main UI...
	//
	private List<View> allView = new ArrayList<View>(); // Save views that need to be registered/unregistered callback....

	public TemplateActivity() {
		// TODO Auto-generated constructor stub
	}	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//
		getApp().Log("TemplateActivity", "onCreate called");
	}
	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		// Views in Activity can receive event callback only in its visible life-cycle...
		if(!allView.isEmpty()) allView.clear();
		reregisterOfView(this.mCurrentView);		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		// Views in Activity will not receive event callback when it is in invisible state or closing...
		if(!allView.isEmpty()) allView.clear();
		unregisterOfView(this.mCurrentView);		
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		getApp().Log("TemplateActivity", "onDestroy called");
	}

	@Override
	public void onBackPressed() {
		String[] aa ={"是","否"};
		Helper.ShowMessage(this, "", "是否離開MIC青年講座？", 2, aa, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				ResourceManager.getInstance().clearAllCache();
				//
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}, null, null);
	}
	
	final public void setView(View view) {
		mCurrentView = view;
	}	
	
	// Get child views, pick up the child (instance of CustomizedView) and insert into allView...
	void getAllView(View view) {
		
		if(!(view instanceof ViewGroup)) {
			// do nothing...
		}
		else {
			ViewGroup group = (ViewGroup) view;
			for(int i = 0; i < group.getChildCount(); i ++) {
				getAllView(group.getChildAt(i));
				if(group.getChildAt(i) instanceof CustomizedView)
					allView.add(group.getChildAt(i));
			}
		}
	}
	

	// Register all views (instance of CustomizedView)...
	void unregisterOfView(View view) {
		getAllView(view);
		for(int i = 0; i < allView.size(); i++) {
			if(allView.get(i) instanceof CustomizedView) {
				((CustomizedView) allView.get(i)).UnregisterCallback();
			}
		}
	}
	
	// Unregister all views (instance of CustomizedView)...
	void reregisterOfView(View view) {
		getAllView(view);
		for(int i = 0; i < allView.size(); i++) {
			if(allView.get(i) instanceof CustomizedView) {
				((CustomizedView) allView.get(i)).ReregisterCallback();
			}
		}
	}	
	
	public iiiApplication getApp(){
		return (iiiApplication)this.getApplicationContext();
	}
	
	//---------------------------------------------------------------------------------------------------------------------------//
	// Option menu will not be supported in this version....so do nothing here......	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	//---------------------------------------------------------------------------------------------------------------------------//
}
